package deprecated.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.SpringUtils;
import domain.dao.interfaces.IDAOAddress;
import domain.entities.impl.Address;
import domain.entities.interfaces.IAddress;

public class DAOAddress extends HibernateDaoSupport implements IDAOAddress {

	private HibernateTemplate hibernateTemplate;


	
	public void init() {
		setHibernateTemplate(getSessionFactory());
	}
	
	public void setHibernateTemplate(SessionFactory sessionFactory){
		this.hibernateTemplate= new HibernateTemplate(sessionFactory);
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOAddress#addAdress(org.hibernate.Session, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public int addAdress(
			final String street, final String city,
			final String zip, final String country) throws Exception {


		ApplicationContext applicationContext = 
				SpringUtils.getApplicationContext();



		IAddress address = (IAddress) applicationContext.getBean("beanAddress");
		address.setStreet(street);
		address.setCity(city);
		address.setZip(zip);
		address.setCountry(country);


		hibernateTemplate.save(address);


		return address.getId();
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOAddress#deleteAddress(org.hibernate.Session, int)
	 */
	public void deleteAddress(int id) throws Exception {

		IAddress address =(IAddress) getHibernateTemplate().get(Address.class, id);
		hibernateTemplate.delete(address);
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOAddress#updateAddress(org.hibernate.Session, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void updateAddress(
			IAddress address,
			int id, final String street, final String city,
			final String zip, final String country) throws Exception {

		address.setStreet(street);
		address.setCity(city);
		address.setZip(zip);
		address.setCountry(country);


		//update into db
		hibernateTemplate.merge(address);
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOAddress#getByContactId(int)
	 */
	public IAddress getByContactId(final int contactId) {

		return (IAddress) hibernateTemplate
				.execute(new HibernateCallback<IAddress>() {

					public IAddress doInHibernate(Session session)
							throws HibernateException, SQLException {

						
						//Business code
						Query query = session.createQuery(
								"SELECT c.add from Contact c"
										+ " WHERE c.id = :contactId");

						query.setParameter("contactId", contactId);
						List result = query.list();
						
						return result.size() > 0 ? 
								(IAddress) query.list().get(0) : 
									null;
					}
				});
	}

}
